<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Userfans extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\UserFans();
    }

    /**
     * 关注列表（type1为我关注的，2为我的粉丝）
     */
    public function lists()
    {
        $type = input('type', 1);
        if (!in_array($type, [1, 2])) {
            $this->error('参数非法！');
        }
        $pageSize = input('pagesize', 15);
        $userId = $this->auth->id;
        switch ($type) {
            case 1:
                $where = [
                    'user_id' => $userId,
                ];

                $list = self::$model->where($where)->order('createtime desc')->paginate($pageSize);
                if ($list) {
                    foreach ($list as $val) {
                        $val->user;
                        $val->fans_num = self::$model->where('fans_user_id', $val->fans_user_id)->count();

                        $id = self::$model->where('fans_user_id', $userId)->where('user_id', $val->fans_user_id)->value('id');
                        $val->is_attention = $id ? true : false;
                    }
                }
                $msg = '我的关注';
                break;
            case 2:
                $where = [
                    'fans_user_id' => $userId,
                ];

                $list = self::$model->where($where)->order('createtime desc')->paginate($pageSize);
                if ($list) {
                    foreach ($list as $val) {
                        $val->fans_user;
                        $id = self::$model->where('user_id', $userId)->where('fans_user_id', $val->fans_user_id)->value('id');
                        $val->is_attention = $id ? true : false;
                    }
                }
                $msg = '我的粉丝';
                break;
        }

        $this->success($msg, $list);
    }

    /**
     * 关注/取消
     */
    public function create()
    {
        $fans_user_id = input('user_id');
        if (!$fans_user_id) {
            $this->error('参数缺失！');
        }

        $userId = $this->auth->id;
        if ($userId == $fans_user_id) {
            $this->error('不能关注自己！');
        }

        $where = [
            'user_id' => $userId,
            'fans_user_id' => $fans_user_id,
        ];

        $row_info = self::$model->where($where)->find();
        if (!$row_info) {
            $fdata = [
                'user_id' => $userId,
                'fans_user_id' => $fans_user_id,
            ];

            $res = self::$model->create($fdata);
            if (!$res) {
                $this->error('关注失败！');
            }
            $this->success('关注成功！');
        } else {
            $res = self::$model->where('id', $row_info->id)->delete();
            if (!$res) {
                $this->error('取关失败！');
            }
            $this->success('取关成功！');
        }
    }
}
